home *** CD-ROM | disk | FTP | other *** search
- #include "lib.h"
-
- #include <errno.h>
- #include <fcntl.h>
-
- #define LOCKDIR "/tmp/" /* or /usr/tmp/ as the case may be */
- #define MAXTRIES 3
- #define NAPTIME 5
- #ifdef ATARI_ST
- #define syserr(X) /* just check errno */
- #ifdef TRUE
- #undef TRUE
- #endif
- #ifdef FALSE
- #undef FALSE
- #endif
- #define TRUE 1
- #define FALSE (~TRUE)
- #define BOOLEAN int
- #else
- /* This is bogus shit unless we have a <lock.h> or something */
- typedef enum { FALSE, TRUE } BOOLEAN;
- #endif
-
- #ifdef __STDC__
- static char *lockpath(_CONST char *);
- #else
- static char *lockpath();
- #endif
-
- BOOLEAN lock(name) /* acquire lock */
- _CONST char *name;
- {
- char *path;
- #ifndef __STDC__
- char *lockpath();
- #endif
- int fd, tries;
- extern int errno;
-
- path = lockpath(name);
- tries = 0;
-
- /* this will work because FS is single threaded, if that is changed
- then it is about as useless */
- while ((fd = open(path, O_WRONLY| O_CREAT | O_EXCL, 0600)) == -1 && errno == EACCES)
-
- {
- if (++tries >= MAXTRIES)
- return(FALSE);
- sleep(NAPTIME);
- }
- if (fd == -1 || close(fd) == -1)
- syserr("lock");
- return(TRUE);
- }
-
- void unlock(name) /* free lock */
- _CONST char *name;
- {
- #ifndef __STDC__
- char *lockpath();
- #endif
-
- if (unlink(lockpath(name)) == -1)
- syserr("unlock");
- }
-
- static char *lockpath(name) /* generate lock file path */
- _CONST char *name;
- {
- static char path[20];
- #ifndef __STDC__ /* for __STDC__ pick up proto from <std.h> */
- char *strcat();
- #endif
-
- strcpy(path, LOCKDIR);
- return(strcat(path, name));
- }
-
-